package com.ymatou.auth.testcase;

import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.auth.parameter.SetDualSecurityRequestDtoBean;
import com.ymatou.auth.service.SetDualSecurityRequestDtoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.AuthCallService;
import com.ymttest.database.model.UserSecurityQuestion;
import com.ymttest.database.sqlwapper.UserSafeWapper;

/**
 * 用户设置了双重验证 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_SetDualSecurityRequestDto {
	private static SetDualSecurityRequestDtoBean setdualsecurityrequestdtoBean;
	private static SetDualSecurityRequestDtoCall setdualsecurityrequestdtoCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("用户设置了双重验证");
	}

	@Before
	public void caseUp() {
		setdualsecurityrequestdtoBean = new SetDualSecurityRequestDtoBean();
		setdualsecurityrequestdtoCall = new SetDualSecurityRequestDtoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(setdualsecurityrequestdtoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_SetDualSecurityRequestDto_001() {
		Logger.start(true, "首次设置用户设置了双重验证成功");
		try {
			int userId = 1;
			UserSafeWapper usw = new UserSafeWapper();
			usw.deleteUserdualsecurity(userId);
			AuthCallService.setSecurityQuestion(userId);
			setdualsecurityrequestdtoBean.setUserId(userId);
			setdualsecurityrequestdtoBean.setEnable(true);
			setdualsecurityrequestdtoCall.setData(setdualsecurityrequestdtoBean);
			setdualsecurityrequestdtoCall.callService();
			Logger.verifyEquals("true", setdualsecurityrequestdtoCall.getString("Success"), "验证Success");
			Logger.verifyEquals("true", setdualsecurityrequestdtoCall.getString("Result"), "验证Result");
			Logger.verifyEquals("200", setdualsecurityrequestdtoCall.getString("ErrorCode"), "验证ErrorCode");
			List<Map> lm = usw.selectUserdualsecurity(userId);
			Logger.verifyNotNull(lm, "数据库数据不为空");
			if (lm != null && lm.size() > 0) {
				Logger.verifyEquals(userId, lm.get(0).get("UserId"), "验证UserId");
				Logger.verifyEquals(1, lm.get(0).get("Enable"), "验证Enable");
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_SetDualSecurityRequestDto_002() {
		Logger.start(true, "不输入usreid，失败");
		try {
			setdualsecurityrequestdtoBean.setEnable(true);
			setdualsecurityrequestdtoCall.setData(setdualsecurityrequestdtoBean);
			setdualsecurityrequestdtoCall.callService();
			Logger.verifyEquals("false", setdualsecurityrequestdtoCall.getString("Success"), "验证Success");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_SetDualSecurityRequestDto_003() {
		Logger.start(true, "不输入enable，失败");
		try {
			setdualsecurityrequestdtoBean.setUserId(1);
			setdualsecurityrequestdtoCall.setData(setdualsecurityrequestdtoBean);
			setdualsecurityrequestdtoCall.callService();
			Logger.verifyEquals("false", setdualsecurityrequestdtoCall.getString("Success"), "验证Success");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_SetDualSecurityRequestDto_004() {
		Logger.start(true, "用户设置了双重验证false成功");
		try {
			int userId = 1;
			UserSafeWapper usw = new UserSafeWapper();
			usw.deleteUserdualsecurity(userId);
			AuthCallService.setSecurityQuestion(userId);
			setdualsecurityrequestdtoBean.setUserId(userId);
			setdualsecurityrequestdtoBean.setEnable(false);
			setdualsecurityrequestdtoCall.setData(setdualsecurityrequestdtoBean);
			setdualsecurityrequestdtoCall.callService();
			Logger.verifyEquals("true", setdualsecurityrequestdtoCall.getString("Success"), "验证Success");
			Logger.verifyEquals("true", setdualsecurityrequestdtoCall.getString("Result"), "验证Result");
			Logger.verifyEquals("200", setdualsecurityrequestdtoCall.getString("ErrorCode"), "验证ErrorCode");
			List<Map> lm = usw.selectUserdualsecurity(userId);
			Logger.verifyNotNull(lm, "数据库数据不为空");
			if (lm != null && lm.size() > 0) {
				Logger.verifyEquals(userId, lm.get(0).get("UserId"), "验证UserId");
				Logger.verifyEquals(0, lm.get(0).get("Enable"), "验证Enable");
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_SetDualSecurityRequestDto_005() {
		Logger.start(true, "重复请求成功,errorcode为202");
		try {
			int userId = 1;
			UserSafeWapper usw = new UserSafeWapper();
			usw.deleteUserdualsecurity(userId);
			AuthCallService.setSecurityQuestion(userId);
			setdualsecurityrequestdtoBean.setUserId(userId);
			setdualsecurityrequestdtoBean.setEnable(false);
			setdualsecurityrequestdtoCall.setData(setdualsecurityrequestdtoBean);
			setdualsecurityrequestdtoCall.callService();
			setdualsecurityrequestdtoCall.callService();
			Logger.verifyEquals("true", setdualsecurityrequestdtoCall.getString("Success"), "验证Success");
			Logger.verifyEquals("true", setdualsecurityrequestdtoCall.getString("Result"), "验证Result");
			Logger.verifyEquals("202", setdualsecurityrequestdtoCall.getString("ErrorCode"), "验证ErrorCode");
			List<Map> lm = usw.selectUserdualsecurity(userId);
			Logger.verifyNotNull(lm, "数据库数据不为空");
			if (lm != null && lm.size() > 0) {
				Logger.verifyEquals(userId, lm.get(0).get("UserId"), "验证UserId");
				Logger.verifyEquals(0, lm.get(0).get("Enable"), "验证Enable");
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_SetDualSecurityRequestDto_006() {
		Logger.start(true, "更新请求成功,errorcode为201");
		try {
			int userId = 1;
			UserSafeWapper usw = new UserSafeWapper();
			usw.deleteUserdualsecurity(userId);
			AuthCallService.setSecurityQuestion(userId);
			setdualsecurityrequestdtoBean.setUserId(userId);
			setdualsecurityrequestdtoBean.setEnable(false);
			setdualsecurityrequestdtoCall.setData(setdualsecurityrequestdtoBean);
			setdualsecurityrequestdtoCall.callService();
			setdualsecurityrequestdtoBean.setEnable(true);
			setdualsecurityrequestdtoCall.callService();
			Logger.verifyEquals("true", setdualsecurityrequestdtoCall.getString("Success"), "验证Success");
			Logger.verifyEquals("true", setdualsecurityrequestdtoCall.getString("Result"), "验证Result");
			Logger.verifyEquals("201", setdualsecurityrequestdtoCall.getString("ErrorCode"), "验证ErrorCode");
			List<Map> lm = usw.selectUserdualsecurity(userId);
			Logger.verifyNotNull(lm, "数据库数据不为空");
			if (lm != null && lm.size() > 0) {
				Logger.verifyEquals(userId, lm.get(0).get("UserId"), "验证UserId");
				Logger.verifyEquals(1, lm.get(0).get("Enable"), "验证Enable");
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_SetDualSecurityRequestDto_007() {
		Logger.start(false, "未设置用户安全问题失败");
		try {
			int userId = 10;
			UserSafeWapper usw = new UserSafeWapper();
			usw.deleteUserdualsecurity(userId);
			setdualsecurityrequestdtoBean.setUserId(userId);
			setdualsecurityrequestdtoBean.setEnable(false);
			setdualsecurityrequestdtoCall.setData(setdualsecurityrequestdtoBean);
			setdualsecurityrequestdtoCall.callService();
			Logger.verifyEquals("true", setdualsecurityrequestdtoCall.getString("Success"), "验证Success");
			Logger.verifyEquals("false", setdualsecurityrequestdtoCall.getString("Result"), "验证Result");
			Logger.verifyEquals("300", setdualsecurityrequestdtoCall.getString("ErrorCode"), "验证ErrorCode");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_SetDualSecurityRequestDto_008() {
		Logger.start(true, "首次设置用户设置了双重验证成功");
		try {
			int userId = 1;
			UserSafeWapper usw = new UserSafeWapper();
			usw.deleteUserdualsecurity(userId);
			AuthCallService.setSecurityQuestion(userId);
			setdualsecurityrequestdtoBean.setUserId(userId);
			setdualsecurityrequestdtoBean.setEnable(true);
			setdualsecurityrequestdtoCall.setData(setdualsecurityrequestdtoBean);
			setdualsecurityrequestdtoCall.callService();
			Logger.verifyEquals("true", setdualsecurityrequestdtoCall.getString("Success"), "验证Success");
			Logger.verifyEquals("true", setdualsecurityrequestdtoCall.getString("Result"), "验证Result");
			Logger.verifyEquals("200", setdualsecurityrequestdtoCall.getString("ErrorCode"), "验证ErrorCode");
			List<Map> lm = usw.selectUserdualsecurity(userId);
			Logger.verifyNotNull(lm, "数据库数据不为空");
			if (lm != null && lm.size() > 0) {
				Logger.verifyEquals(userId, lm.get(0).get("UserId"), "验证UserId");
				Logger.verifyEquals(1, lm.get(0).get("Enable"), "验证Enable");
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_SetDualSecurityRequestDto_009() {
		Logger.start(false, "错误的userid失败");
		try {
			int userId = -10;
			UserSafeWapper usw = new UserSafeWapper();
			usw.deleteUserdualsecurity(userId);
			setdualsecurityrequestdtoBean.setUserId(userId);
			setdualsecurityrequestdtoBean.setEnable(false);
			setdualsecurityrequestdtoCall.setData(setdualsecurityrequestdtoBean);
			setdualsecurityrequestdtoCall.callService();
			Logger.verifyEquals("false", setdualsecurityrequestdtoCall.getString("Success"), "验证Success");
			Logger.verifyEquals("false", setdualsecurityrequestdtoCall.getString("Result"), "验证Result");
			Logger.verifyEquals("100", setdualsecurityrequestdtoCall.getString("ErrorCode"), "验证ErrorCode");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}